package cn.tedu.lonnghospital.appointment.service.impl;

import cn.tedu.lonnghospital.appointment.ex.ServiceException;
import cn.tedu.lonnghospital.appointment.mapper.AppointmentMapper;
import cn.tedu.lonnghospital.appointment.mapper.DiagnosisMapper;
import cn.tedu.lonnghospital.appointment.pojo.dto.DiagnosisAddNewDTO;
import cn.tedu.lonnghospital.appointment.pojo.entity.Appointment;
import cn.tedu.lonnghospital.appointment.pojo.entity.Diagnosis;
import cn.tedu.lonnghospital.appointment.pojo.vo.DiagnosisAddNewVO;
import cn.tedu.lonnghospital.appointment.pojo.vo.DiagnosisStandardVO;
import cn.tedu.lonnghospital.appointment.service.DiagnosisService;
import cn.tedu.lonnghospital.appointment.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DiagnosisServiceImpl implements DiagnosisService {
    @Autowired
    AppointmentMapper appointmentMapper;
    @Autowired
    DiagnosisMapper diagnosisMapper;
    @Override
    public DiagnosisAddNewVO getAddNew(long appointmentId) {
        QueryWrapper<Appointment> qwa=new QueryWrapper<>();
        qwa.eq("id",appointmentId).eq("appointment_status","已预约");
        Integer count=appointmentMapper.selectCount(qwa);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该预约不存在！");
        }
        DiagnosisAddNewVO diagnosisAddNewVO=diagnosisMapper.getAddNew(appointmentId);
        return diagnosisAddNewVO;
    }

    @Override
    public void addNew(DiagnosisAddNewDTO diagnosisAddNewDTO) {
        QueryWrapper<Diagnosis> qwd=new QueryWrapper<>();
        qwd.eq("appointment_id",diagnosisAddNewDTO.getAppointmentId());
        Integer count=diagnosisMapper.selectCount(qwd);
        if(count>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该次就诊病历已存在！请直接修改病历！");
        }
        QueryWrapper<Appointment> qwa=new QueryWrapper<>();
        qwa.eq("id",diagnosisAddNewDTO.getAppointmentId()).eq("appointment_status","已预约");
        count=appointmentMapper.selectCount(qwa);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该预约不存在！");
        }
        Diagnosis diagnosis=new Diagnosis();
        BeanUtils.copyProperties(diagnosisAddNewDTO,diagnosis);
        diagnosis.setVisitTime(LocalDateTime.now());
        Integer rows=diagnosisMapper.insert(diagnosis);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"服务器繁忙，请稍后再试！");
        }
    }

    @Override
    public DiagnosisStandardVO getStandardById(long diagnosisId) {
        QueryWrapper<Diagnosis> qwd=new QueryWrapper<>();
        qwd.eq("id",diagnosisId);
        Integer count=diagnosisMapper.selectCount(qwd);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"查询失败！就诊ID不存在！");
        }
        return diagnosisMapper.getStandardById(diagnosisId);
    }

    @Override
    public void updateById(long diagnosisId, String record) {
        QueryWrapper<Diagnosis> qwd=new QueryWrapper<>();
        qwd.eq("id",diagnosisId);
        Integer count=diagnosisMapper.selectCount(qwd);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"查询失败！就诊ID不存在！");
        }
        Diagnosis diagnosis=new Diagnosis();
        diagnosis.setId(diagnosisId).setRecord(record);
        Integer rows=diagnosisMapper.updateById(diagnosis);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"服务器繁忙请稍后再试！");
        }
    }
}
